package com.goldgov.information.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gtiles.components.securityworkbench.bean.SwbAuthUser;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.goldgov.addressbook.service.AddressBookGroupQuery;
import com.goldgov.column.service.Column;
import com.goldgov.column.service.ColumnInit;
import com.goldgov.column.service.ColumnQuery;
import com.goldgov.information.service.Article;
import com.goldgov.information.service.ArticleQuery;
import com.goldgov.information.service.ArticleRules;
import com.goldgov.information.service.ArticleRulesQuery;
import com.goldgov.information.service.ArticleRulesService;
import com.goldgov.information.service.ArticleService;
import com.goldgov.information.service.ArticleTagLink;
import com.goldgov.information.service.ArticleTagLinkQuery;
import com.goldgov.information.service.ArticleTagLinkService;
import com.goldgov.information.service.CrawlerLog;
import com.goldgov.information.service.CrawlerLogQuery;
import com.goldgov.information.service.CrawlerLogService;
import com.goldgov.kcloud.core.json.JsonErrorObject;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.login.web.model.LoginAuthUser;
import com.goldgov.ms.adapter.call.RemoteCallService;
import com.goldgov.utils.CmsUtils;
import com.goldgov.utils.CrawlerUtils;
import com.goldgov.utils.CrawlerUtilsTest;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.JsonUtil;
import com.goldgov.utils.PropertyUtil;
import com.goldgov.utils.UrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;

/** 
 * @ClassName:InformationController
 * @Description: 信息采集
 * @author MaN
 * @date 2018年7月11日 
 */

@SuppressWarnings("deprecation")
@RestController
@RequestMapping("/workbench/information")
@Api("信息发布接口")
public class InformationController {

	@Autowired
	private ArticleService articleService;
	
	@Autowired
	private ArticleTagLinkService articleTagLinkService;
	
	@Autowired
	private ArticleRulesService articleRulesService;
	
	@Autowired
	private CrawlerLogService crawlerLogService;
	
	@Autowired
	private RemoteCallService remoteCallService;
	
	protected final Log logger = LogFactory.getLog(this.getClass());
	
	@RequestMapping("/listArticle")
	@ApiOperation(value = "采集结果审核列表", notes ="")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchAuditState", value = "采集状态", paramType = "query"),
		 @ApiImplicitParam(name = "searchTitle", value = "栏目名称", paramType = "query"),
		 @ApiImplicitParam(name = "searchBeginDate", value = "发布开始时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchEndDate", value = "发布结束时间", paramType = "query")})
	public JsonQueryObject<Article> listArticle(@ApiIgnore ArticleQuery query,HttpServletRequest request){
		List<Article> list=articleService.getArticleList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping
	@ApiOperation(value = "发布新闻列表", notes ="")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchTitle", value = "文章标题", paramType = "query"),
		 @ApiImplicitParam(name = "searchCategoryName", value = "栏目名称", paramType = "query"),
		 @ApiImplicitParam(name = "searchBeginDate", value = "发布开始时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchEndDate", value = "发布结束时间", paramType = "query")})
	public JsonQueryObject<Article> listAuditArticle(@ApiIgnore ArticleQuery query,HttpServletRequest request){
		query.setSearchAuditState(ArticleQuery.ACTIVESTATE_YES);
		List<Article> list=articleService.getArticleList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@PostMapping("/auditArticle")
	@ApiOperation(value = "打标签", notes ="")
	public JsonObject<Object> auditArticle(Article obj,HttpServletRequest request){
		//删除文章标签关联表
		articleTagLinkService.deleteArticleTagLink(obj.getArticleID());
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		String[] tags=obj.getTagIDs();
		if(null!=tags && tags.length>0) {
			for(String tag:tags) {
				ArticleTagLink link=new ArticleTagLink();
				link.setTagID(tag);
				link.setArticleID(obj.getArticleID());
				link.setUserID(u.getEntityID());
				link.setUpdateTime(new Date());
				articleTagLinkService.addArticleTagLink(link);
			}
		}
		obj.setLastModifyTime(new Date());
		articleService.updateArticle(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@PostMapping("/addArticle")
	@ApiOperation(value = "新增文章", notes ="")
	public JsonObject<Object> addArticle(Article obj,HttpServletRequest request) throws IOException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d=new Date();
		SwbAuthUser u=LoginAuthUser.getUserByAdmin(request);
		if (PropertyUtil.objectNotEmpty(obj.getPublishState()) && obj.getPublishState().intValue() == Article.PUBLISH_STATE_YES.intValue()) {
			obj.setPublishDate(new Date());
		}
		obj.setActiveState(ArticleQuery.ACTIVESTATE_YES);
		obj.setLastModifyTime(d);
		obj.setCreateDate(d);
		obj.setUserID(u.getEntityID());
		obj.setUserName(u.getUserName());
		obj.setStickState(ArticleQuery.ACTIVESTATE_NO);
		obj.setAuditState(ArticleQuery.ACTIVESTATE_YES);
		obj.setAuditDate(new Date());
		articleService.addArticle(obj);
		//发布到cms
		if(PropertyUtil.objectNotEmpty(obj.getPublishState()) && obj.getPublishState().intValue() == Article.PUBLISH_STATE_YES.intValue()) {
			Map<String,String> params=new HashMap<>();
			String articleAuthor= obj.getArticleAuthor();
			if(null!=articleAuthor && !"".equals(articleAuthor)) {
				params.put("articleAuthor", articleAuthor);
				//当为爬取数据时，创建人为数据来源
				params.put("basicPeopleName", obj.getArticleSource());
			}
			String articleContent= obj.getArticleContent();
			if(null!=articleContent && !"".equals(articleContent)) {
				params.put("articleContent", articleContent);
			}
			String articleSource= obj.getArticleSource();
			if(null!=articleSource && !"".equals(articleSource)) {
				params.put("articleSource", articleSource);
			}
			Integer basicSort= obj.getArticleSort();
			if(null!=basicSort) {
				params.put("basicSort", basicSort.toString());
			}
			String articleKeyword= obj.getArticleKeyWord();
			if(null!=articleKeyword && !"".equals(articleKeyword)) {
				params.put("articleKeyword", articleKeyword);
			}
			String articleTitle= obj.getArticleTitle();
			if(null!=articleTitle && !"".equals(articleTitle)) {
				params.put("basicTitle", articleTitle);
			}
			if(null!=obj.getPublishDateText() && !"".equals(obj.getPublishDateText())) {
				params.put("articlePublishDateText", obj.getPublishDateText());
			}
			if(null!=obj.getArticleUrl() && !"".equals(obj.getArticleUrl())) {
				params.put("articleSite", obj.getArticleUrl());
			}
			if(null!=obj.getArticleUrl() && !"".equals(obj.getArticleUrl())) {
				params.put("articleSite", obj.getArticleUrl());
			}
			params.put("basicDateTime",obj.getPublishDate()==null?sdf.format(d):sdf.format(obj.getPublishDate()));
			params.put("basicCategoryId", obj.getCmsCategoryID()==null?"154":obj.getCmsCategoryID().toString());
			params.put("articleStick", obj.getStickState()==null?"2":obj.getStickState().toString());
			params.put("articleAttribute", obj.getArticleAttribute().toString());
			params.put("articleStatus", Article.PUBLISH_STATE_FABU.toString());
			params.put("isGrab", Article.ARTICLE_IS_GRAB.toString());
			Connection conn = Jsoup.connect(UrlUtil.CMS_ARTICLE_URL).ignoreContentType(true);
            conn.data(params);
            conn.post();

            //CMS静态化
            new CmsUtils().start();
		}
		return JsonSuccessObject.SUCCESS;
	}
	
	@PostMapping("/editArticle")
	@ApiOperation(value = "编辑文章", notes ="")
	public JsonObject<Object> editArticle(Article obj,HttpServletRequest request) throws Exception{
		if (PropertyUtil.objectNotEmpty(obj.getPublishState()) && obj.getPublishState().intValue() == Article.PUBLISH_STATE_YES.intValue()) {
			if (PropertyUtil.objectNotEmpty(obj.getPublishDate())) {
				obj.setPublishDate(obj.getPublishDate());
			}else {
				obj.setPublishDate(new Date());
			}
		}else {
			if (PropertyUtil.objectNotEmpty(obj.getPublishDate())) {
				obj.setPublishDate(obj.getPublishDate());
			}
		}
		if (PropertyUtil.objectNotEmpty(obj.getAuditState()) && obj.getAuditState().intValue() == Article.PUBLISH_STATE_YES.intValue()) {
			obj.setAuditDate(new Date());
		}else {
			obj.setAuditDate(null);
		}
		obj.setLastModifyTime(new Date());
		articleService.updateArticle(obj);
		
		//发布到cms
		if(PropertyUtil.objectNotEmpty(obj.getPublishState()) && obj.getPublishState().intValue() == Article.PUBLISH_STATE_YES.intValue()) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d=new Date();
			Map<String,String> params=new HashMap<>();
			String articleAuthor= obj.getArticleAuthor();
			if(null!=articleAuthor && !"".equals(articleAuthor)) {
				params.put("articleAuthor", articleAuthor);
				//当为爬取数据时，创建人为数据来源
				params.put("basicPeopleName", obj.getArticleSource());
			}
			String articleContent= obj.getArticleContent();
			if(null!=articleContent && !"".equals(articleContent)) {
				params.put("articleContent", articleContent);
			}
			String articleSource= obj.getArticleSource();
			if(null!=articleSource && !"".equals(articleSource)) {
				params.put("articleSource", articleSource);
			}
			Integer basicSort= obj.getArticleSort();
			if(null!=basicSort) {
				params.put("basicSort", basicSort.toString());
			}
			String articleKeyword= obj.getArticleKeyWord();
			if(null!=articleKeyword && !"".equals(articleKeyword)) {
				params.put("articleKeyword", articleKeyword);
			}
			String articleTitle= obj.getArticleTitle();
			if(null!=articleTitle && !"".equals(articleTitle)) {
				params.put("basicTitle", articleTitle);
			}
			if(null!=obj.getPublishDateText() && !"".equals(obj.getPublishDateText())) {
				params.put("articlePublishDateText", obj.getPublishDateText());
			}
			if(null!=obj.getArticleUrl() && !"".equals(obj.getArticleUrl())) {
				params.put("articleSite", obj.getArticleUrl());
			}
			params.put("basicDateTime",obj.getPublishDate()==null?sdf.format(d):sdf.format(obj.getPublishDate()));
			params.put("basicCategoryId", obj.getCmsCategoryID()==null?"154":obj.getCmsCategoryID().toString());
			params.put("articleStick", obj.getStickState()==null?"2":obj.getStickState().toString());
			params.put("articleAttribute", obj.getArticleAttribute().toString());
			params.put("articleStatus", Article.PUBLISH_STATE_FABU.toString());
			params.put("isGrab", Article.ARTICLE_IS_GRAB.toString());
			Connection conn = Jsoup.connect(UrlUtil.CMS_ARTICLE_URL).ignoreContentType(true);
            conn.data(params);
            conn.post();
            
            //CMS静态化
            new CmsUtils().start();
		}
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/deleteArticle")
	@ApiOperation(value = "删除文章", notes ="")
	public JsonObject<Object> deleteArticle(@ApiParam(value = "articleID", required = true) @RequestParam("articleID") String articleID,HttpServletRequest request){
		Article obj=articleService.getArticleByID(articleID);
		Article a = new Article();
		a.setLastModifyTime(new Date());
		a.setActiveState(ArticleQuery.ACTIVESTATE_NO);
		a.setArticleID(obj.getArticleID());
		articleService.updateArticle(a);
		
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/getArticleByID")
	@ApiOperation(value = "文章详情", notes ="")
	public JsonSuccessObject<Article> getArticleByID(@ApiParam(value = "articleID", required = true) @RequestParam("articleID") String articleID,HttpServletRequest request){
		Article obj=articleService.getArticleByID(articleID);
		JsonSuccessObject<Article> returnResult = new JsonSuccessObject<Article>(obj);
		return returnResult;
	}
	@GetMapping("/previewArticleByID")
	@ApiOperation(value = "文章详情", notes ="")
	public JsonSuccessObject<Article> previewArticleByID(@ApiParam(value = "articleID", required = true) @RequestParam("articleID") String articleID,HttpServletRequest request){
		Article obj=articleService.getArticleByID(articleID);
		if (PropertyUtil.objectNotEmpty(obj.getPublishDate())) {
			obj.setPublishDateStr(DateUtil.dateToStr(obj.getPublishDate()));
		}
		JsonSuccessObject<Article> returnResult = new JsonSuccessObject<Article>(obj);
		return returnResult;
	}
	
	@GetMapping("/stickArticle")
	@ApiOperation(value = "置顶/取消置顶", notes ="")
	public JsonObject<Object> stickArticle(@ApiParam(value = "articleID", required = true) @RequestParam("articleID") String articleID,HttpServletRequest request){
		Article art=articleService.getArticleByID(articleID);
		art.setLastModifyTime(new Date());
		Integer stickState=art.getStickState();
		if(null!=stickState && stickState==1) {
			art.setStickState(ArticleQuery.ACTIVESTATE_NO);
		}else {
			art.setStickState(ArticleQuery.ACTIVESTATE_YES);
		}
		try {
			articleService.updateArticle(art);
			return JsonSuccessObject.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return JsonErrorObject.ERROR;
		}
		
	}
	
	@PostMapping("/publishArticles")
	@ApiOperation(value = "发布文章到cms", notes ="")
	public JsonObject<Object> publishArticles(@ApiParam(value = "articleIDs", required = true) @RequestParam("ids") String[] ids,HttpServletRequest request) throws IOException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(null!=ids && ids.length>0) {
			for(int i=0;i<ids.length;i++) {
				Article obj=articleService.getArticleByID(ids[i]);
				obj.setPublishState(AddressBookGroupQuery.ACTIVESTATE_YES);
				obj.setPublishDate(new Date());
				articleService.updateArticle(obj);
				Map<String,String> params=new HashMap<>();
				String articleAuthor= obj.getArticleAuthor();
				if(null!=articleAuthor && !"".equals(articleAuthor)) {
					params.put("articleAuthor", articleAuthor);
				}
				String articleContent= obj.getArticleContent();
				if(null!=articleContent && !"".equals(articleContent)) {
					params.put("articleContent", articleContent);
				}
				String articleSource= obj.getArticleSource();
				if(null!=articleSource && !"".equals(articleSource)) {
					params.put("articleSource", articleSource);
				}
				Integer basicSort= obj.getArticleSort();
				if(null!=basicSort) {
					params.put("basicSort", basicSort.toString());
				}
				String articleKeyword= obj.getArticleKeyWord();
				if(null!=articleKeyword && !"".equals(articleKeyword)) {
					params.put("articleKeyword", articleKeyword);
				}
				String articleTitle= obj.getArticleTitle();
				if(null!=articleTitle && !"".equals(articleTitle)) {
					params.put("basicTitle", articleTitle);
				}
				if(null!=obj.getPublishDateText() && !"".equals(obj.getPublishDateText())) {
					params.put("articlePublishDateText", obj.getPublishDateText());
				}
				if(null!=obj.getArticleUrl() && !"".equals(obj.getArticleUrl())) {
					params.put("articleSite", obj.getArticleUrl());
				}
				params.put("basicDateTime",obj.getPublishDate()==null?sdf.format(new Date()):sdf.format(obj.getPublishDate()));
				params.put("basicCategoryId", obj.getCmsCategoryID()==null?"154":obj.getCmsCategoryID().toString());
				params.put("articleStick", obj.getStickState()==null?"2":obj.getStickState().toString());
				params.put("articleAttribute", obj.getArticleAttribute().toString());
				params.put("articleStatus", Article.PUBLISH_STATE_FABU.toString());
				params.put("isGrab", Article.ARTICLE_IS_GRAB.toString());
				Connection conn = Jsoup.connect(UrlUtil.CMS_ARTICLE_URL).ignoreContentType(true);
	            conn.data(params);
	            conn.post();
			}
			//CMS静态化
			new CmsUtils().start();
		}
		return JsonSuccessObject.SUCCESS;
	}
	
	@PostMapping("/addArticleRules")
	@ApiOperation(value = "新增采集规则", notes ="")
	public JsonObject<Object> addArticleRules(ArticleRules obj,HttpServletRequest request){
		String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		obj.setLastModifyDate(date);
		obj.setActiveState(ArticleQuery.ACTIVESTATE_YES);
		articleRulesService.addArticleRules(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@PostMapping("/updateArticleRules")
	@ApiOperation(value = "修改采集规则", notes ="")
	public JsonObject<Object> updateArticleRules(ArticleRules obj,HttpServletRequest request){
		String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		obj.setLastModifyDate(date);
		articleRulesService.updateArticleRules(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/deleteArticleRules")
	@ApiOperation(value = "删除采集规则", notes ="")
	public JsonObject<Object> deleteArticleRules(@ApiParam(value = "rulesIDs", required = true) @RequestParam("rulesIDs") String[] rulesIDs,HttpServletRequest request){
		if (PropertyUtil.objectNotEmpty(rulesIDs)) {
			for (String rulesID : rulesIDs) {
				ArticleRules obj=articleRulesService.getArticleRulesByID(rulesID);
				String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				obj.setLastModifyDate(date);
				obj.setActiveState(ArticleQuery.ACTIVESTATE_NO);
				articleRulesService.updateArticleRules(obj);
				//清空日志
				crawlerLogService.deleteCrawlerLogByRulesID(rulesID);
			}
			return JsonSuccessObject.SUCCESS;
		}else {
			return JsonErrorObject.ERROR;
		}
	}
	
	@GetMapping("/listArticleRules")
	@ApiOperation(value = "采集规则列表", notes ="")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchUrlName", value = "目标网站名称", paramType = "query"),
		 @ApiImplicitParam(name = "searchUrlAddress", value = "目标网站地址", paramType = "query"),
		 @ApiImplicitParam(name = "searchBeginDate", value = "最后运行开始时间", paramType = "query"),
		 @ApiImplicitParam(name = "searchEndDate", value = "最后运行结束时间", paramType = "query")})
	public JsonQueryObject<ArticleRules> listArticleRules(@ApiIgnore ArticleRulesQuery query,HttpServletRequest request){
		List<ArticleRules> list=articleRulesService.getArticleRulesList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@GetMapping("/startReptilian")
	@ApiOperation(value = "启动信息采集", notes ="")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "rulesID", value = "规则ID", paramType = "query"),
		@ApiImplicitParam(name = "runningState", value = "启动or停止", paramType = "query")})
	@Deprecated
	public JsonObject<Object> startReptilian(@ApiIgnore ArticleRules obj,HttpServletRequest request){
		//更新规则
		obj.setLastOperationDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		articleRulesService.updateArticleRules(obj);
		return JsonSuccessObject.SUCCESS;
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@GetMapping("/runReptilian")
	@ApiOperation(value = "开始抓取信息", notes ="")
	@ApiImplicitParams({@ApiImplicitParam(name = "rulesID", value = "规则ID", paramType = "query")})
	@Deprecated
	public JsonObject<Object> runReptilian(@ApiIgnore ArticleRules obj,HttpServletRequest request){
		//更新规则
		obj.setLastOperationDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		articleRulesService.updateArticleRules(obj);
		//调用爬虫方法
		try {
				new CrawlerUtils(obj.getRulesID());
				return JsonSuccessObject.SUCCESS;
		}catch(Exception e) {
			e.printStackTrace();
			return JsonErrorObject.ERROR;
		}
	}
	
	@GetMapping("/findArticleTagList")
	@ApiOperation(value = "查询已关联的标签", notes ="")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchArticleID", value = "文章id", paramType = "query")})
	public JsonQueryObject<ArticleTagLink> findArticleTagList(@ApiIgnore ArticleTagLinkQuery query,HttpServletRequest request){
		List<ArticleTagLink> list=articleTagLinkService.findArticleTagList(query);
		query.setResultList(list);
		return new JsonQueryObject<>(query);
	}
	
	@GetMapping("/getRulesByID")
	@ApiOperation(value = "获取采集规则详情", notes ="")
	public JsonSuccessObject<ArticleRules> getRulesByID(@ApiParam(value = "rulesID", required = true) @RequestParam("rulesID") String rulesID,HttpServletRequest request){
		ArticleRules obj=articleRulesService.getArticleRulesByID(rulesID);
		return  new JsonSuccessObject<ArticleRules>(obj);
	}
	
	@GetMapping("/getColumns")
	@ApiOperation(value = "获取门户下的栏目集合", notes ="")
	public JsonQueryObject<Column> getColumns(@ApiParam(value = "categoryId", required = true) @RequestParam("categoryId") String categoryId,HttpServletRequest request){
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("categoryId", categoryId);
		params.put("searchColumnTitle", "");
		params.put("columnAttribute", 0);
		String resultList=remoteCallService.getForObject("findAllColumn", String.class, params);
		JSONObject json=JSONObject.parseObject(resultList);//.getJSONObject("page");
		JSONArray jsonList=json.getJSONArray("list");
		List<Column> columnList=JSONArray.parseArray(jsonList.toJSONString(),Column.class);
	    ColumnQuery query=new ColumnQuery();
	    query.setResultList(columnList);
		return new JsonQueryObject<>(query);
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@PostMapping("/deleteArticleByIDs")
	@ApiOperation(value = "删除文章集合", notes ="")
	public JsonObject<Object> deleteArticleByIDs(@ApiParam(value = "articleIDs", required = true) @RequestParam("ids") String[] ids){
		try {
			articleService.deleteArticleByIDs(ids);
//		    Map<String,String> params=new HashMap<>();
//		    StringBuffer articleIDs=new StringBuffer();
//		    for(String s:ids) {
//		    	articleIDs.append(s+",");
//		    }
//			params.put("articleIDs", articleIDs.toString());
//			remoteCallService.getForObject("deleteArticle", String.class, params);
			return JsonUtil.success();
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.error();
		}
		
	}
	@PostMapping("/cancelArticles")
	@ApiOperation(value = "撤销发布文章", notes ="")
	public JsonObject<Object> cancelArticles(@ApiParam(value = "articleIDs", required = true) @RequestParam("ids") String[] ids){
		try {
			articleService.cancelArticles(ids);
//		    Map<String,String> params=new HashMap<>();
//		    StringBuffer articleIDs=new StringBuffer();
//		    for(String s:ids) {
//		    	articleIDs.append(s+",");
//		    }
//			params.put("articleIDs", articleIDs.toString());
//			remoteCallService.getForObject("cancelArticle", String.class, params);
			return JsonUtil.success();
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.error();
		}
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@DeleteMapping("/deleteArticlesByRulesID")
	@ApiOperation(value = "根据规则ID批量删除文章", notes ="删除文章")
	public JsonObject<Object> deleteArticlesByRulesID(@ApiParam(value = "RulesID", required = true) @RequestParam("id") String id){
		articleService.deleteArticlesByRulesID(id);
		return JsonSuccessObject.SUCCESS;
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@PostMapping("/deleteCrawlerLogByIDs")
	@ApiOperation(value = "根据日志ID删除爬虫日志", notes ="删除日志")
	public JsonObject<Object> deleteCrawlerLogByIDs(@ApiParam(value = "logIDs", required = true) @RequestParam("ids") String[] ids){
		crawlerLogService.deleteCrawlerLogByIDs(ids);
		return JsonSuccessObject.SUCCESS;
	}
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@GetMapping("/findNoAudit")
	@ApiOperation(value = "根据日志ID删除爬虫日志", notes ="删除日志")
	public JsonSuccessObject<Integer> findNoAudit(@ApiParam(value = "logIDs", required = true) @RequestParam("ids") String[] ids){
		int auditNum = articleService.findNoAudit(ids);//判断是否存在未审核的数据
		Integer status = 1;//可以发布
		if (auditNum > 0) {
			status = 2;//存在未审核的数据
		}else {
			if (articleService.findPublish(ids) > 0) {
				status = 3;//存在已发布的数据
			}else {
				status = 1;
			}
		}
		return new JsonSuccessObject<Integer>(status);
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@DeleteMapping("/deleteCrawlerLogByRulesID")
	@ApiOperation(value = "根据规则ID批量删除日志", notes ="删除日志")
	public JsonObject<Object> deleteCrawlerLogByRulesID(@ApiParam(value = "rulesID", required = true) @RequestParam("id") String id){
		crawlerLogService.deleteCrawlerLogByRulesID(id);
		return JsonSuccessObject.SUCCESS;
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@GetMapping("/getCrawlerLogList")
	@ApiOperation(value = "获取爬虫规则下的爬虫日志", notes ="列表查询")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchRulesID", value = "规则ID", paramType = "query")})
	public JsonQueryObject<CrawlerLog> getCrawlerLogList(@ApiIgnore CrawlerLogQuery query,HttpServletRequest request){
		query.setResultList(crawlerLogService.getCrawlerLogList(query));
		return new JsonQueryObject<>(query);
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@GetMapping("/getCrawlerLogCount")
	@ApiOperation(value = "获取爬虫规则下的总数据量", notes ="查询")
	@ApiImplicitParams({ @ApiImplicitParam(name = "searchRulesID", value = "规则ID", paramType = "query")})
	public JsonSuccessObject<CrawlerLog> getCrawlerLogCount(@ApiIgnore CrawlerLogQuery query,HttpServletRequest request){
		CrawlerLog obj=new CrawlerLog();
		obj.setRulesID(query.getSearchRulesID());
		obj=crawlerLogService.getCrawlerLogCount(obj);
		return new JsonSuccessObject<>(obj);
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@SuppressWarnings("unchecked")
	@GetMapping("/testCrawlerExpression")
	@ApiOperation(value = "测试爬虫表达式", notes ="测试测试")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "testUrl", value = "测试网址", paramType = "query"),
		@ApiImplicitParam(name = "testRule", value = "爬虫表达式", paramType = "query"),
		@ApiImplicitParam(name = "testArticleUrl", value = "文章地址规则", paramType = "query"),
	})
	@Deprecated
	public JsonQueryObject<ArticleRules> testCrawlerExpression(@ApiIgnore ArticleRulesQuery query,HttpServletRequest request){
		JSONObject json=new JSONObject();
		json.put("_level", 1);
		json.put("_url", query.getTestUrl());
		json.put("_rule", query.getTestRule());
		json.put("_articleUrlRule",query.getTestArticleUrl());
		Request requests = new Request();
		requests.putExtra("_rules", json);
		requests.setUrl(json.get("_url").toString());
		Spider s=Spider.create(new CrawlerUtilsTest()).addRequest(requests);
		s.run();
		query.setResultList(s.getTestResult());
		return new JsonQueryObject<>(query);
	}
	
	/**
	 * @author RongSL
	 * @Date 2018/08/17
	 * @version 1.0.0
	 * */
	@PostMapping("/runReptilianByCategoryId")
	@ApiOperation(value = "根据栏目批量抓取文章", notes ="抓取文章")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "searchCmsCategoryID", value = "栏目ID", paramType = "query")
	})
	@Deprecated
	public JsonObject<Object> runReptilianByCategoryId(@ApiIgnore ArticleRulesQuery query,HttpServletRequest request){
		//根据栏目ID查询相关规则
		List<ArticleRules> rules=articleRulesService.getArticleRulesList(query);
		//判断规则是否为空
		if(rules.isEmpty()) {
			JsonErrorObject.ERROR.setCode("100");
			JsonErrorObject.ERROR.setMessage("该栏目下尚未配置任何抓取规则");
			return JsonErrorObject.ERROR;
		}else {
			ArticleRules a;
			Integer count=0;
			for (ArticleRules ar : rules) {
				//为了减少更新字段提升性能所以new
				a = new ArticleRules();
				a.setRulesID(ar.getRulesID());
				// 更新规则操作时间
				a.setLastOperationDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				articleRulesService.updateArticleRules(a);
				// 调用爬虫方法
				try {
					Request requests = new Request();
					requests.putExtra("_level", 1);
					requests.putExtra("_rules", JSON.toJSONString(ar));
					requests.setUrl(ar.getUrlAddress());
					CrawlerUtils.setCount(0);
					CrawlerUtils.setRulesID(a.getRulesID());
					Spider s=Spider.create(new CrawlerUtils()).addRequest(requests).thread(10);
					s.run();
					//累计每次爬取数据量
					count += CrawlerUtils.getCount();
				} catch (Exception e) {
					logger.error("========根据栏目批量抓取文章异常", e);
				}
			}
			JsonSuccessObject.SUCCESS.setCode("200");
			JsonSuccessObject.SUCCESS.setMessage("本次成功抓取"+count+"条文章");
			return JsonSuccessObject.SUCCESS;
		}
	}
	
	/**日爬取量对比图
	 * @Date 2018/12/18
	 * */
	@GetMapping("/statisticsRPQL")
	@ApiOperation(value = "日爬取量对比图", notes ="统计图")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query")
	})
	public JsonObject<Object> statisticsRPQL(@ApiIgnore ArticleRulesQuery query,HttpServletRequest request){
		List<ArticleRules> list=articleRulesService.statisticsRPQL(query);
		JsonSuccessObject.SUCCESS.setData(list);
		return JsonSuccessObject.SUCCESS;
	}
	
	/**栏目爬取量对比图
	 * @Date 2018/12/18
	 * */
	@GetMapping("/statisticsLMPQL")
	@ApiOperation(value = "栏目爬取量对比图", notes ="统计图")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "searchBeginDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query")
	})
	public JsonObject<Object> statisticsLMPQL(@ApiIgnore ArticleRulesQuery query,HttpServletRequest request){
		List<ArticleRules> list=articleRulesService.statisticsLMPQL(query);
		for(ArticleRules ar:list){
			if(ColumnInit.getCategoryidconverttitle().containsKey(ar.getCmsCategoryID().toString())) {
				ar.setCmsCategoryName(ColumnInit.getCategoryidconverttitle().get(ar.getCmsCategoryID().toString()));
			}
		}
		JsonSuccessObject.SUCCESS.setData(list);
		return JsonSuccessObject.SUCCESS;
	}
	
//	public static void main(String[] args) {
//		JSONObject json=new JSONObject();
//		json.put("_url", "http://dangjian.people.com.cn/GB/395446/index.html");
//		json.put("_rule", "2,/html/body/div[7]/div[1]/div/p[2]/a/text()");
//		json.put("_authorUrlRule","/html/body/div[5]/div[1]/ul/li/a/@href");
//		json.put("_level", 1);
//		Request requests = new Request();
//		requests.putExtra("_rules", json);
//		requests.setUrl(json.get("_url").toString());
//		Spider s=Spider.create(new CrawlerUtilsTest()).addRequest(requests);
//		s.run();
//		System.out.println(s.getTestResult().get(0));
//	}
}